package com.amumu.drama.portal.modules.order.controller;

import com.amumu.drama.common.base.api.CommonResult;
import com.amumu.drama.common.constant.OperateTypeEnum;
import com.amumu.drama.common.base.dto.req.IdReq;
import com.amumu.drama.common.modules.delivery.service.DeliveryService;
import com.amumu.drama.common.modules.order.dto.req.QueryPayStatusReq;
import com.amumu.drama.common.modules.order.dto.resp.PayStatusResp;
import com.amumu.drama.common.modules.order.service.PaymentService;
import com.amumu.drama.common.modules.service.impl.payment.PaymentReq;
import com.amumu.drama.common.modules.service.impl.payment.PaymentResp;
import com.amumu.drama.portal.util.MemberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@Api(tags = "第三方回调接口")
@Tag(name = "第三方回调接口", description = "支付接口")
@RequestMapping("/api/v1/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private DeliveryService deliveryService;

    @ApiOperation("用户-轮询查询订单支付成功状态接口")
    @PostMapping("/queryPayStatus")
    public CommonResult<PayStatusResp> queryPayStatus(QueryPayStatusReq request) {
        return CommonResult.success(paymentService.queryPayStatus(request, OperateTypeEnum.MEMBER, MemberUtil.getCurrentMemberId()));
    }

    @ApiOperation("第三方系统-支付回调")
    @PostMapping("/notify/{payHandlerType}")
    public Object payNotify(@PathVariable("payHandlerType") String payHandlerType,
                            @RequestParam(required = false) Map<String, String> params,
                            @RequestBody(required = false) String body) {
        log.info("pay notify，payHandlerType:{} params:{},body:{}", payHandlerType, params, body);
        try {
            return paymentService.payNotify(payHandlerType, params, body);
        } catch (Exception e) {
            log.error("pay notify error", e);
        }
        return null;
    }


    @ApiOperation("第三方系统-支付退款回调")
    @PostMapping("/refundNotify/{payHandlerType}")
    public Object refundNotify(@PathVariable("payHandlerType") String payHandlerType,
                               @RequestParam(required = false) Map<String, String> params,
                               @RequestBody(required = false) String body) {
        log.info("pay notify，payHandlerType:{} params:{},body:{}", payHandlerType, params, body);
        try {
            return paymentService.refundNotify(payHandlerType, params, body);
        } catch (Exception e) {
            log.error("refund notify error", e);
        }
        return null;
    }

    @ApiOperation("第三方系统-发货回调")
    @PostMapping("/deliveryNotify/{deliveryType}")
    public Object deliveryNotify(@PathVariable("deliveryType") String deliveryType,
                                 @RequestParam(required = false) Map<String, String> params,
                                 @RequestBody(required = false) String body) {
        log.info("deliveryNotify[{}}] params:{},body:{}", deliveryType, params, body);
        try {
            return deliveryService.deliveryNotify(deliveryType, params, body);
        } catch (Exception e) {
            log.error("delivery Notify error", e);
        }
        return null;
    }

    @ApiOperation("用户退款接口")
    @PostMapping("/refund")
    public CommonResult<PaymentResp> refund(@RequestBody @Validated IdReq request) {
        PaymentReq paymentReq = new PaymentReq();
        paymentReq.setOrderId(request.getId());
        return CommonResult.success(paymentService.refundApply(paymentReq, OperateTypeEnum.MEMBER, MemberUtil.getCurrentMemberId()));
    }

//    /**
//     * 创建预支付订单接口
//     */
//    @PostMapping("/prePayment")
//    public CommonResult<PrePaymentResp> prePayment(PrePaymentReq request) {
//        return CommonResult.success(paymentService.prePayment(request));
//    }

//    /**
//     * 支付接口
//     *
//     * @param umsMember
//     * @param paymentReq
//     * @param request
//     * @return
//     */
//    @PostMapping("/pay")
//    public CommonResult<PaymentResp> pay(UmsMember umsMember, PaymentReq paymentReq, HttpServletRequest request) {
//        try {
//            paymentReq.setIp(HttpUtils.getRealIp(request));
//            return CommonResult.success(paymentService.pay(umsMember, paymentReq));
//        } catch (Exception e) {
//            log.error("{} pay error", paymentReq.getOrderId(), e);
//            return CommonResult.failed("支付失败");
//        }
//    }


}
